<?php


namespace App\Engine\ali;


use Darabonba\OpenApi\Models\Config;
use App\Models\SysConfig;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;

class Message
{
    private $config;
    private $dysmsapi;
    private $sys;

    public function __construct()
    {
        $sys=SysConfig::get_sys_obj(['accesskey_id','accesskey_secret','ali_sign','ali_mode_id']);
        $this->sys=$sys;
        $this->config=new Config([
            'accessKeyId'=>$sys['accesskey_id'],
            'accessKeySecret'=>$sys['accesskey_secret']
        ]);
        $this->config->endpoint="dysmsapi.aliyuncs.com";
        $this->dysmsapi=new Dysmsapi($this->config);
    }

    /**发端验证码到手机
     * @param $phone
     * @param $code
     */
    public function sendsmsCode($phone,$code)
    {
        $data=[
            'code'=>$code
        ];
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $phone,
            "signName" => $this->sys['ali_sign'],
            "templateCode" => $this->sys['ali_mode_id'],
            'templateParam'=>json_encode($data)
        ]);
        $this->dysmsapi->sendSms($sendSmsRequest);
    }
}
